@echo off
set IMAGE_NAME=ppt-remote-controller
set CONTAINER_NAME=ppt-remote-controller
set TEMP_IMAGE_NAME=%IMAGE_NAME%-temp

echo ========================================
echo PPT Remote Controller - Docker Redeploy
echo ========================================
echo.

echo --- Step 1: Building new temporary image: %TEMP_IMAGE_NAME% ---
docker build -t %TEMP_IMAGE_NAME% .
if %errorlevel% neq 0 (
    echo Docker build failed!
    pause
    exit /b 1
)
echo.

echo --- Step 2: Stopping and removing existing container: %CONTAINER_NAME% ---
docker stop %CONTAINER_NAME% >nul 2>&1
docker rm %CONTAINER_NAME% >nul 2>&1
echo.

echo --- Step 3: Removing old image: %IMAGE_NAME% ---
docker rmi %IMAGE_NAME% >nul 2>&1
echo.

echo --- Step 4: Tagging temporary image to final name: %IMAGE_NAME% ---
docker tag %TEMP_IMAGE_NAME% %IMAGE_NAME%
docker rmi %TEMP_IMAGE_NAME%
echo.

echo --- Step 5: Running new container: %CONTAINER_NAME% ---
docker run -d --name %CONTAINER_NAME% ^
    -p 7663:7663 ^
    --restart unless-stopped ^
    %IMAGE_NAME%

if %errorlevel% neq 0 (
    echo Failed to start container!
    pause
    exit /b 1
)
echo.

echo ========================================
echo Redeployment complete!
echo ========================================
echo.
echo Container is running on port 7663
echo Access URL: http://localhost:7663
echo.
echo To view logs: docker logs -f %CONTAINER_NAME%
echo To stop: docker stop %CONTAINER_NAME%
echo.
pause
